package 刷题.leedcode622;
//设计循环队列
public class MyCircularQueue {
    private ListNode head;
    private ListNode tail;//尾结点
    private int capacity;//容量
    private int size;
    //构造器，设置队列长度为k
    public MyCircularQueue(int k) {
        capacity = k;
        size = 0;
    }
    //向循环队列插入一个元素，如果成功则返回真
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        ListNode node = new ListNode(value);
        if(head == null){
            head = tail = node;
        }else{
            tail.next = node;
            tail = node;
        }
        size++;
        return true;
    }
    //从循环队列中删除一个元素，成功返回真
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        ListNode node = head;
        head = head.next;
        size--;
        return true;
    }
    //从队首获取元素，如果队列为空，返回-1
    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return head.val;
    }
    //获取队尾元素，如果队列为空，返回-1
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return tail.val;
    }
    //检查循环队列是否为空
    public boolean isEmpty() {
        return size == 0;
    }
    //检查循环队列是否已满
    public boolean isFull() {
        return size == capacity;
    }
}
